package com.rzht.framework.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.rzht.framework.App;
import com.rzht.framework.R;


/**
 * error(img)为加载图片错误时显示的
 * placeholder(img)为图片加载中显示的
 * crossFade()为淡入淡出动画，有重载方法，输入int值为x毫秒，默认300
 * dontAnimate()不使用动画效果
 * resize(x,y) 调整图片大小
 * asgif()如果调用这个方法，图片不是gif的话，会认为是加载失败并调用error()占位符
 * asBitmap()方法如果调用，图片如果是gif的话，只会播放第一帧，其他图片正常显示
 * .....................................
 * diskCacheStrategy()
 * DiskCacheStrategy.NONE 什么都不缓存，
 * DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像。
 * DiskCacheStrategy.RESULT 仅仅缓存最终的图像
 * DiskCacheStrategy.ALL 缓存所有版本的图像（默认行为）
 * ..............................
 * Priority()请求图片优先级（升序）（优先级并不是完全严格遵守的）
 * Priority.LOW
 * Priority.NORMAL
 * Priority.HIGH
 * Priority.IMMEDIATE
 */
public class ImageUtils {


    /**
     * 加载图片
     *
     * @param imageView
     * @param url
     */
    public static void loadImage(Context context, final ImageView imageView, final String url) {
        Glide.with(App.getContext()).load(url).placeholder(R.drawable.ic_launcher_background).error(R.mipmap.ic_launcher).diskCacheStrategy(DiskCacheStrategy.RESULT).crossFade().into(imageView);
    }

    public static void loadImage(Context context, ImageView imageView, String path, Transformation transformation) {
        Glide.with(context).load(path).placeholder(R.drawable.ic_launcher_background).bitmapTransform(transformation).diskCacheStrategy(DiskCacheStrategy.RESULT).error(R.mipmap.ic_launcher).into(imageView);
    }

    public static void loadLocalImage(Context context, ImageView imageView, String path) {
        Glide.with(context)
                .load(path)
                .placeholder(R.drawable.ic_launcher_background)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(imageView);
    }

    public static void loadLocalImage(Context context, ImageView imageView, int imgId) {
        Glide.with(context).load(imgId).placeholder(R.drawable.ic_launcher_background).diskCacheStrategy(DiskCacheStrategy.RESULT).into(imageView);
    }

    public static void loadLocalImage(Context context, ImageView imageView, int imgId, Transformation transformation) {
        Glide.with(App.getContext())
                .load(imgId)
//                .placeholder(R.drawable.loading)
                .bitmapTransform(transformation)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .error(R.mipmap.ic_launcher)
                .into(imageView);
    }


    public static void displayRoundImage(final Context context, final ImageView imageView, String url) {
        if (StringUtils.isEmpty(url)) {
            return;
        }
        Glide.with(context).load(url).asBitmap().centerCrop().placeholder(R.drawable.ic_launcher_background).error(R.mipmap.ic_launcher).into(new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        });
    }

    /**
     * 清除缓存
     */
    public static void cleanCache() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(App.getContext()).clearDiskCache();
            }
        }).start();
        Glide.get(App.getContext()).clearMemory();
    }

}
